package com.hk.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.Nav;
import com.hk.model.OnlineMessage;
import com.hk.model.Product;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IOnlineMessageService;
import com.hk.service.impl.OnlineMessageServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/21 16:14
 * @Statement: 查询所有留言列表和添加留言信息
 */
@WebServlet("/message/MessageQueryOrSave")
public class MessageQueryOrSaveServlet extends HttpServlet {

    IOnlineMessageService iOnlineMessageService = new OnlineMessageServiceImpl();

    /**
     * 查询所有留言
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String title = Tool.nullToString(req.getParameter("title"));
        String email = Tool.nullToString(req.getParameter("email"));
        String gmtCreate = Tool.nullToString(req.getParameter("gmtCreate"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
//        封装数据
        OnlineMessage onlineMessage = new OnlineMessage();
        onlineMessage.setTitle(title);
        onlineMessage.setEmail(email);
        onlineMessage.setGmtCreate(gmtCreate);
//        System.out.println("product="+product);
//        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
        List<OnlineMessage> onlineMessageList = iOnlineMessageService.findAll(pageNum,size,onlineMessage);
        int count = iOnlineMessageService.count(-1,-1, onlineMessage);
        PrintWriter out = resp.getWriter();
        if (onlineMessageList.isEmpty()) {
            ResultVO<OnlineMessage> resultVO = new ResultVO<>(0,"error",0,onlineMessageList);
            String onlineMessageListJSON = JSONObject.toJSONString(resultVO);
            out.println(onlineMessageListJSON);
        } else {
            ResultVO<OnlineMessage> resultVO = new ResultVO<>(0,"success",count,onlineMessageList);
            String onlineMessageListJSON = JSONObject.toJSONString(resultVO);
            out.println(onlineMessageListJSON);
        }
        out.flush();
        out.close();

    }

    /**
     * 添加留言信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        OnlineMessage onlineMessage = gson.fromJson(reader, OnlineMessage.class);
//        判断是否获取到值并转换成功，No则创建新对象
        if (onlineMessage == null) {
            onlineMessage = new OnlineMessage();
        }
        PrintWriter out = resp.getWriter();
        Result result = new Result();
//        判断是否获取到值
        if (onlineMessage.getTitle() == "" || onlineMessage.getTitle() == null ) {
            result.setStatus(0);
            result.setMessage("error");
            String resultJSON = JSONObject.toJSONString(result);
            out.println(resultJSON);
            out.flush();
            out.close();
        }
        int save = iOnlineMessageService.save(onlineMessage);
        if (save != 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();

    }
}
